package com.lcg.word.bean

import android.database.sqlite.SQLiteDatabase
import android.os.Parcelable
import com.j256.ormlite.android.AndroidConnectionSource
import com.j256.ormlite.dao.Dao
import com.j256.ormlite.dao.DaoManager
import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.table.DatabaseTable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize
import java.io.File


@DatabaseTable(tableName = "book")
@Parcelize
data class BookDb(
    @DatabaseField(generatedId = true)
    var id: Long? = null,
    @DatabaseField
    var name: String? = null
) : Parcelable

@Parcelize
@DatabaseTable
class BookUnit(
    @DatabaseField(generatedId = true)
    var id: Long? = null,
    @DatabaseField
    var name: String? = null,
    @DatabaseField(canBeNull = false, foreign = true)
    var book: BookDb? = null,
    @DatabaseField
    var pass: Boolean = false
) : Parcelable

@Parcelize
@DatabaseTable
class Word(
    @DatabaseField(generatedId = true)
    var id: Long? = null,
    @DatabaseField
    var en: String? = null,
    @DatabaseField
    var cn: String? = null,
    @DatabaseField
    var audio: String? = null,
    /**00000....1### 表示出现3次，后3位表示对错情况*/
    @DatabaseField
    var listening: Long = 1L,
    @DatabaseField
    var lastTimeListening: Long = 0L,
    /**00000....1### 表示出现3次，后3位表示对错情况*/
    @DatabaseField
    var speaking: Long = 1L,
    @DatabaseField
    var lastTimeSpeaking: Long = 0L,
    /**00000....1### 表示出现3次，后3位表示对错情况*/
    @DatabaseField
    var reading: Long = 1L,
    @DatabaseField
    var lastTimeReading: Long = 0L,
    /**00000....1### 表示出现3次，后3位表示对错情况*/
    @DatabaseField
    var writing: Long = 1L,
    @DatabaseField
    var lastTimeWriting: Long = 0L,
    @DatabaseField(canBeNull = false, foreign = true)
    var book: BookDb? = null,
    @DatabaseField(canBeNull = false, foreign = true)
    var unit: BookUnit? = null,
) : Parcelable {
    fun getCN() = cn?.replace("/[^/]+/".toRegex(), "")?.trim()

    fun getUrl() = "https://thumb.1010pic.com/dmt/audio$audio"

    /**书写正确次数*/
    val writingRight: Int by lazy {
        var right = -1
        repeat(128) {
            if (writing and (1L shl it) != 0L) {
                right++
            }
        }
        if (right == -1) right = 0
        right
    }

    /**书写次数*/
    val writingCount: Int by lazy {
        var count = 0
        repeat(128) {
            if (writing and (1L shl it) != 0L) {
                count = it
            }
        }
        count
    }
}

lateinit var bookDao: Dao<BookDb, *>
lateinit var unitDao: Dao<BookUnit, *>
lateinit var wordDao: Dao<Word, *>

/**初始化DAO*/
suspend fun initDao(file: File) = withContext(Dispatchers.IO) {
    try {
        val database =
            SQLiteDatabase.openDatabase(file.path, null, SQLiteDatabase.OPEN_READWRITE)
        val source = AndroidConnectionSource(database)
        bookDao = DaoManager.createDao(source, BookDb::class.java)
        unitDao = DaoManager.createDao(source, BookUnit::class.java)
        wordDao = DaoManager.createDao(source, Word::class.java)
        true
    } catch (e: Exception) {
        false
    }
}